{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "1019.ipynb",
      "provenance": [],
      "collapsed_sections": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/yananma/5_programs_per_day/blob/master/1019.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4_DnA598rfL_",
        "colab_type": "text"
      },
      "source": [
        "# 1"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7Qitn5CYtKYu",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%matplotlib inline "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "1X-6Hb2_tcml",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torch \n",
        "import torch.nn as nn \n",
        "import torch.nn.functional as F"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "mpjsGgtrtlVM",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "class Net(nn.Module):\n",
        "    def __init__(self):\n",
        "        super(Net, self).__init__()\n",
        "        self.conv1 = nn.Conv2d(1, 6, 5)\n",
        "        self.conv2 = nn.Conv2d(6, 16, 5)\n",
        "        self.fc1 = nn.Linear(16 * 5 * 5, 120)\n",
        "        self.fc2 = nn.Linear(120, 84)\n",
        "        self.fc3 = nn.Linear(84, 10)\n",
        "\n",
        "    def forward(self, x):\n",
        "        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))\n",
        "        x = F.max_pool2d(F.relu(self.conv2(x)), 2)\n",
        "        x = x.view(-1, self.num_flat_features(x))\n",
        "        x = F.relu(self.fc1(x))\n",
        "        x = F.relu(self.fc2(x))\n",
        "        x = self.fc3(x)\n",
        "        return x \n",
        "    \n",
        "    def num_flat_features(self, x):\n",
        "        size = x.size()[1:]\n",
        "        num_features = 1 \n",
        "        for s in size:\n",
        "            num_features *= s \n",
        "        return num_features "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "wNmwKcrkuw-V",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "net = Net()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7S65RsmevLqi",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print(net)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gJpfCc0Tvwl8",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "net.parameters()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fhG7hDOywLqs",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "params = list(net.parameters())\n",
        "print(params)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5Ua5yJmwwUaC",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print(len(params))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Gq13dBZtwdKk",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "z = torch.ones(2, 3, 4, 5)\n",
        "print(z)\n",
        "print(len(z))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "iEllGsIzwrNY",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print(params[0])"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "SInnr3UDxDmt",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "input = torch.randn(1, 1, 32, 32)\n",
        "print(input)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "x9WKo_DkwXzk",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print(len(input))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "o8v0248mwkRa",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "out = net(input)\n",
        "print(out)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "FDd-ckY1w0cp",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "net.zero_grad()\n",
        "out.backward(torch.randn(1, 10))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "tz0DFcqZxTtG",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "output = net(input)\n",
        "target = torch.randn(10)\n",
        "target = target.view(1, -1)\n",
        "criterion = nn.MSELoss()\n",
        "\n",
        "loss = criterion(output, target)\n",
        "print(loss)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "AJNFiTNdx_Su",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "a = torch.tensor([0., 0., 0.])\n",
        "b = torch.tensor([3., 3., 1.])\n",
        "print(a, b)\n",
        "criterion(a, b)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "i7A_CbXVyEgm",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print(loss.grad_fn)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "U98EXtsA9PGw",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print(loss.grad_fn.next_functions[0][0])"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "bvMWCqGf9UEK",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print(loss.grad_fn.next_functions[0][0].next_functions[0][0])"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "M1kalREI9g1J",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print(loss.grad_fn.next_functions[0][0].next_functions[0][0].next_functions)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ZPDUztvg9qgN",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "net.zero_grad()\n",
        "print(net.conv1.bias.grad)\n",
        "\n",
        "loss.backward()\n",
        "\n",
        "print(net.conv1.bias.grad)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "InD1quCm-UlD",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "learning_rate = 0.01 "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "g0_99QfL_NQt",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "for f in net.parameters():\n",
        "    f.data.sub_(f.grad.data * learning_rate)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Rv-OTlqt_Ug6",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torch.optim as optim \n",
        "optimizer = optim.SGD(net.parameters(), lr=0.01)\n",
        "\n",
        "optimizer.zero_grad()\n",
        "output = net(input)\n",
        "loss = criterion(output, target)\n",
        "loss.backward()\n",
        "optimizer.step()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "USy3enB8_3ta",
        "colab_type": "text"
      },
      "source": [
        "# 2"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "FRAFKOJAACjy",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torch\n",
        "import torch.nn as nn\n",
        "import torchvision\n",
        "import torchvision.transforms as transforms\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "import matplotlib.pyplot as plt\n",
        "%matplotlib inline"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8p96eohzAQlh",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "transform = transforms.Compose(\n",
        "    [transforms.ToTensor(), \n",
        "     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]\n",
        ")"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "VYzN5WPPLCN3",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform = transform)\n",
        "trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=2)\n",
        "\n",
        "testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)\n",
        "testloader = torch.utils.data.DataLoader(testset, batch_size=4, shuffle=False, num_workers=2)\n",
        "\n",
        "classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "kEXWDEMHMIty",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "def imshow(img):\n",
        "    img = img / 2 + 0.5 \n",
        "    npimg = img.numpy()\n",
        "    plt.imshow(np.transpose(npimg, (1, 2, 0)))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "VUF3SHpBMyvB",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "dataiter = iter(trainloader)\n",
        "images, labels = dataiter.next()\n",
        "\n",
        "imshow(torchvision.utils.make_grid(images))\n",
        "\n",
        "print([' '.join('%5s' % classes[labels[j]] for j in range(4))])"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "6TK9geY_NQH4",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torch.nn as nn \n",
        "import torch.nn.functional as F \n",
        "\n",
        "class Net(nn.Module):\n",
        "    def __init__(self):\n",
        "        super(Net, self).__init__()\n",
        "        self.conv1 = nn.Conv2d(3, 6, 5)\n",
        "        self.pool = nn.MaxPool2d(2, 2)\n",
        "        self.conv2 = nn.Conv2d(6, 16, 5)\n",
        "        self.fc1 = nn.Linear(16 * 5 * 5, 120)\n",
        "        self.fc2 = nn.Linear(120, 84)\n",
        "        self.fc3 = nn.Linear(84, 10)\n",
        "\n",
        "    def forward(self, x):\n",
        "        x = self.pool(F.relu(self.conv1(x)))\n",
        "        x = self.pool(F.relu(self.conv2(x)))\n",
        "        x = x.view(-1, 16 * 5 * 5)\n",
        "        x = F.relu(self.fc1(x))\n",
        "        x = F.relu(self.fc2(x))\n",
        "        x = self.fc3(x)\n",
        "        return x \n",
        "\n",
        "\n",
        "net = Net()\n",
        "print(net)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "_VLZd3ZBRPdx",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torch.optim as optim \n",
        "\n",
        "criterion = nn.CrossEntropyLoss()\n",
        "optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "pyNiRP8KSdWY",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "a = torch.tensor([[0.3, 1., 0.1], \n",
        "        [0.3, 0.1, 1.]])\n",
        "b = torch.tensor([1, 2])\n",
        "\n",
        "criterion(a, b)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ykmwhuVVTr5S",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "for i, data in enumerate(testloader, 0):\n",
        "    inputs, labels = data \n",
        "    outputs1 = net(inputs)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gwIM-1bXU8a4",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print(inputs.size())"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "dDOg5HXGWSym",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print(labels)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ubchwqEjXCSu",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print(inputs.size())"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HukkieMvYx1I",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print(outputs1)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "APQWW5B0ZAUV",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print(labels)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "r3PqZZD_ZHkD",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "criterion(outputs1, labels)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "UTToR-LlZRGr",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "for epoch in range(2):\n",
        "    running_loss = 0.0 \n",
        "    for i, data in enumerate(trainloader, 0):\n",
        "        inputs, labels = data \n",
        "\n",
        "        optimizer.zero_grad()\n",
        "\n",
        "        outputs = net(inputs)\n",
        "        loss = criterion(outputs, labels)\n",
        "        loss.backward()\n",
        "        optimizer.step()\n",
        "\n",
        "        running_loss += loss.item()\n",
        "        if i % 2000 == 1999:\n",
        "            print('[%d, %5d] Loss: %.3f' % (epoch+1, i+1, running_loss / 2000))\n",
        "            running_loss = 0.0 \n",
        "\n",
        "print(\"Finished Training\")"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ZV3xW1XT5fBG",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "dataiter = iter(testloader)\n",
        "images, labels = dataiter.next()\n",
        "\n",
        "imshow(torchvision.utils.make_grid(images))\n",
        "print('GroundTruth: ', ' '.join('%5s' % classes[labels[j]] for j in range(4)))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ywJ0ZKkQ7Iwu",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "outputs = net(images)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "nWXlJMN6Ek4a",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "correct = 0 \n",
        "total = 0 \n",
        "with torch.no_grad():\n",
        "    for data in testloader:\n",
        "        images, labels = data \n",
        "        outputs = net(images)\n",
        "        _, predicted = torch.max(outputs.data, 1)\n",
        "        total += labels.size(0)\n",
        "        correct += (predicted == labels).sum().item()\n",
        "\n",
        "print('Accuracy: %d %%' % (100 * correct / total))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "3OzWU1dPFHrH",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "class_correct = list(0. for i in range(10))\n",
        "class_total = list(0. for i in range(10))\n",
        "with torch.no_grad():\n",
        "    for data in testloader:\n",
        "        images, labels = data \n",
        "        outputs = net(images)\n",
        "        _, predicted = torch.max(outputs, 1)\n",
        "        c = (predicted == labels).squeeze()\n",
        "        for i in range(4):\n",
        "            label = labels[i]\n",
        "            class_correct[label] += c[i].item()\n",
        "            class_total[label] += 1"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ooXWoI94HLtX",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "for i in range(10):\n",
        "    print('Accuracy of %5s: %2d %%' %(classes[i], 100 * class_correct[i] / class_total[i]))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Gg3kcWNWH0SK",
        "colab_type": "text"
      },
      "source": [
        "# 3"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "63DmWcB4KbNB",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torch \n",
        "import numpy as np \n",
        "\n",
        "torch.__version__"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "iFIDhJA1KnAk",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "x = torch.rand(2, 3)\n",
        "print(x)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5bkRvS-rLBcz",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print(x.size())"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "AKkMACTNLPwp",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "y = torch.rand(2, 3, 4, 5)\n",
        "print(y.size())\n",
        "print(y)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "lg_MfiUMMCTQ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "x = torch.tensor([3.14])\n",
        "print(x)\n",
        "x.size()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HckBXoC4MQEZ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "x.item()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "pA3cYMTkMXTM",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "long = x.long()\n",
        "long"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "x3XAXzZ6Mrki",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "half = x.half()\n",
        "half"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "KeJIhG3tM7Ww",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "int_t = x.int()\n",
        "int_t"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Wxd2lW_dNBWr",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "flo = x.float()\n",
        "flo"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "f2_QcHUSNFjd",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "a = torch.randn((3, 2))\n",
        "print(a)\n",
        "numpy_a = a.numpy()\n",
        "print(numpy_a)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "rptYfAyUNWZP",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "torch_a = torch.from_numpy(numpy_a)\n",
        "torch_a"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "35feJrB2NktJ",
        "colab_type": "text"
      },
      "source": [
        "# 4"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "mhntqaEfPti6",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torch\n",
        "torch.__version__"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GGd0xfnsP1i2",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "[x ** 2 for x in range(5) if x > 2]"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "_GGPl8n0RsNL",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "x = torch.rand(5, 5, requires_grad=True)\n",
        "y = torch.rand(5, 5, requires_grad=True)\n",
        "print(x)\n",
        "print(y)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hHqLCsU6UJxj",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "z = torch.sum(x + y)\n",
        "z "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "84XFg1jeUROD",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "z.backward()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "6YG4L3fNUZOU",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print(x.grad)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "MF5bfGWHUa6j",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print(y.grad)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "uvOtGGqHUdbr",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "z = x ** 2 + y ** 3\n",
        "z"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "115AOBfsU2Yy",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "z.backward(torch.ones_like(x))\n",
        "print(x.grad)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "diknTdD3U9I7",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "dir(z)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "b7pdptNxVl3r",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "x.is_leaf"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "MtG-Bu9yVzKt",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "z.grad_fn.next_functions"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "F4GYLIU3WDAi",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "z.grad_fn.next_functions[0][0]"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WCCxIimpWMYK",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from torch.autograd.function import Function "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "25zvacdVWthV",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "class MulConstant(Function):\n",
        "    @staticmethod\n",
        "    def forward(ctx, tensor, constant):\n",
        "        ctx.constant=constant \n",
        "        return tensor * constant \n",
        "    @staticmethod \n",
        "    def backward(ctx, grad_output):\n",
        "        return grad_output, None "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "a8rgEIEVXPl1",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "a = torch.rand(3, 3, requires_grad=True)\n",
        "b = MulConstant.apply(a, 5)\n",
        "print('a:'+str(a))\n",
        "print('b:'+str(b))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "AO-yG3hXXijI",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "b.backward(torch.ones_like(a))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "s7x8JjAKX5Dl",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "a.grad"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ENE_obIhX6Po",
        "colab_type": "text"
      },
      "source": [
        "# 5"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "uEe5czPoX86s",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torch \n",
        "import torch.nn as nn \n",
        "torch.__version__"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "agym2z4DhRd2",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torch.nn.functional as F"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "E2DgSSlZhYTu",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "class Net(nn.Module):\n",
        "    def __init__(self):\n",
        "        super(Net, self).__init__()\n",
        "        self.conv1 = nn.Conv2d(1, 6, 3)\n",
        "        self.fc1 = nn.Linear(1350, 10)\n",
        "\n",
        "    def forward(self, x):\n",
        "        print(x.size())\n",
        "        x = self.conv1(x)\n",
        "        print(x.size())\n",
        "        x = F.relu(x)\n",
        "        print(x.size())\n",
        "        x = F.max_pool2d(x, (2, 2))\n",
        "        print(x.size())\n",
        "        x = F.relu(x)\n",
        "        print(x.size())\n",
        "        x = x.view(x.size()[0], -1)\n",
        "        print(x.size())\n",
        "        x = self.fc1(x)\n",
        "        return x \n",
        "\n",
        "\n",
        "net = Net()\n",
        "print(net)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "1ZBiLJ-8jBfG",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "for parameters in net.parameters():\n",
        "    print(parameters)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fFFvmQwCjNUJ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "for name, parameters in net.named_parameters():\n",
        "    print(name, ':', parameters.size())"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "pZlt2BwZjfLQ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "input = torch.randn(1, 1, 32, 32)\n",
        "out = net(input)\n",
        "out.size()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "K6SqNU-RqTKI",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "net.zero_grad()\n",
        "out.backward(torch.ones(1, 10))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hcmUpXX5qh3J",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "y = torch.arange(0, 10).view(1, 10).float()\n",
        "criterion = nn.MSELoss()\n",
        "loss = criterion(out, y)\n",
        "print(loss.item())"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "6KNOFLHgrKQB",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torch.optim \n",
        "\n",
        "out = net(input)\n",
        "criterion = nn.MSELoss()\n",
        "loss = criterion(out, y)\n",
        "optimizer = torch.optim.SGD(net.parameters(), lr=0.01)\n",
        "optimizer.zero_grad()\n",
        "loss.backward()\n",
        "optimizer.step()\n",
        "\n",
        "print(loss.item())"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "AtYiaSrIr8YQ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}